﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.IO;
using System.Text;

namespace Qiao.Common.Core.Tests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            #region 加密
            string myData = "hello";
            string myPassword = "OpenSesame1111111111111111";
            byte[] cipherText;
            byte[] salt = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x0 };

            var key = new Rfc2898DeriveBytes(myPassword, salt);
            // Encrypt the data.
            var algorithm = new RijndaelManaged();
            algorithm.Key = key.GetBytes(16);
            algorithm.IV = key.GetBytes(16);
            var sourceBytes = new System.Text.UnicodeEncoding().GetBytes(myData);
            using (var sourceStream = new MemoryStream(sourceBytes))
            using (var destinationStream = new MemoryStream())
            using (var crypto = new CryptoStream(sourceStream, algorithm.CreateEncryptor(), CryptoStreamMode.Read))
            {
                moveBytes(crypto, destinationStream);
                cipherText = destinationStream.ToArray();
            }
            string str = Convert.ToBase64String(cipherText);
            #endregion
            //string str = "11111";
            //Assert.AreEqual(str, str);
        }
        private void moveBytes(Stream source, Stream dest)
        {
            byte[] bytes = new byte[2048];
            var count = source.Read(bytes, 0, bytes.Length);
            while (0 != count)
            {
                dest.Write(bytes, 0, count);
                count = source.Read(bytes, 0, bytes.Length);
            }
        }
        #region 列表 ListHelper

        #region ListExtend

        #region 集合转字符串
        [TestMethod]
        public void ListToStringTestMethod()
        {
            List<string> list = new List<string>();
            list.Add("123");
            list.Add("321");
            list.Add("1");

            string str = list.ListToString(",");
            Assert.AreEqual(str, "123,321,1");
        }
        #endregion

        public bool IsListSatisfactionConditions111(List<string> list, Func<List<string>, bool> conditions)
        {
            return list.Any();
        }
        #endregion

        #endregion

        #region Demo

        #region 泛型方法的写法

        [TestMethod]
        public void DemoFunc1TestMethod()
        {
            DemoFunc1();
        }

        public void DemoFunc1()
        {
            Func<List<string>, bool> func = (i) => i == null;
            ///Func<List<string>, bool> 中只有两个类型 其中
            ///     List<string> 是数据源
            ///     bool    是返回值类型
            ///func 是这个委托的变量名
            ///(i)  是类型List<stirng> 的变量名
            ///i==null  是返回的结果
            var sss = func(new List<string>());
            var list = new List<string>() { null, "123", "123", null, null };
            sss = func(null);


        }
        #endregion



        #endregion

    }
}
